package tom.client.admin;

import tom.client.Library;
import tom.client.MainPanel;
import tom.client.WindMill;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.TextBox;

public class FormPassword extends AbsolutePanel {

	@Override
	public void setTitle(String title) {
		pwdNew.setText(null);
		pwdOld.setText(null);
		pwdConfirm.setText(null);
		pwdOld.setFocus(true);
	}

	public void setParams(String userName) {
		txtUserName.setText(userName);
	}

	public FormPassword() {
		setSize("800px", "500px");

		pwdOld = new PasswordTextBox();
		add(pwdOld, 185, 66);
		pwdOld.setSize("150px", "20px");

		pwdNew = new PasswordTextBox();
		add(pwdNew, 185, 113);
		pwdNew.setSize("150px", "20px");

		pwdConfirm = new PasswordTextBox();
		add(pwdConfirm, 185, 147);
		pwdConfirm.setSize("150px", "20px");

		Label lblNewLabel = new Label("รหัสผ่านเดิม");
		add(lblNewLabel, 84, 73);

		Label label = new Label("รหัสผ่านใหม่");
		add(label, 84, 119);
		label.setSize("95px", "21px");

		Label label_1 = new Label("ยืนยันรหัสใหม่");
		add(label_1, 84, 153);
		label_1.setSize("95px", "21px");

		Button btnPassword = new Button("เปลี่ยนรหัสผ่าน");
		btnPassword.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (pwdNew.getText() != null && !pwdNew.getText().equals(""))
					if (pwdNew.getText().equals(pwdConfirm.getText())) {
						Library.getAdminService().updatePassword(
								txtUserName.getText(),
								Library.encode(pwdOld.getText()),
								Library.encode(pwdNew.getText()), pwdCallback);
					} else {
						Window.alert("รหัสผ่านใหม่ ยืนยันไม่ตรงกัน");
					}
				if (passwd2New.getText() != null && !passwd2New.getText().equals(""))
					if (passwd2New.getText().equals(passwd2Confirm.getText())) {
						Library.getAdminService().updatePassword2(
								txtUserName.getText(),
								Library.encode(passwd2Old.getText()),
								Library.encode(passwd2New.getText()), pwdCallback);
					} else {
						Window.alert("รหัสผ่าน2ใหม่ ยืนยันไม่ตรงกัน");
					}
			}
		});
		add(btnPassword, 185, 291);

		Label lblNewLabel_1 = new Label("ชื่อ login");
		add(lblNewLabel_1, 84, 36);

		txtUserName = new TextBox();
		txtUserName.setReadOnly(true);
		add(txtUserName, 185, 31);
		txtUserName.setSize("150px", "20px");
		
		add(passwd2Confirm, 538, 147);
		passwd2Confirm.setSize("150px", "20px");

		add(passwd2New, 538, 113);
		passwd2New.setSize("150px", "20px");

		Label label_2 = new Label("รหัสผ่าน2ใหม่");
		add(label_2, 437, 119);
		label_2.setSize("95px", "21px");

		Label label_3 = new Label("ยืนยันรหัส2ใหม่");
		add(label_3, 437, 153);
		label_3.setSize("95px", "21px");

		Label label_4 = new Label("รหัสผ่าน2 เดิม");
		add(label_4, 437, 73);
		label_4.setSize("95px", "18px");
		
		add(passwd2Old, 538, 66);
		passwd2Old.setSize("150px", "20px");
	}

	private static FormPassword panel;
	private final PasswordTextBox pwdConfirm;
	private final PasswordTextBox pwdNew;
	private final PasswordTextBox pwdOld;
	private final PasswordTextBox passwd2Confirm = new PasswordTextBox();
	private final PasswordTextBox passwd2New = new PasswordTextBox();
	private final PasswordTextBox passwd2Old = new PasswordTextBox();

	public static FormPassword getPanel() {
		if (panel == null)
			panel = GWT.create(FormPassword.class);
		return panel;
	}
	
	protected AsyncCallback<Boolean> pwdCallback = new AsyncCallback<Boolean>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(Boolean result) {
			if (result) {
				MainPanel.mainPanel.back();
				WindMill.setStatus("บันทึกรหัสผ่านเรียบร้อย");
			}
		}
	};
	private final TextBox txtUserName;
}
